layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','laydate','upload'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var upload = layui.upload;

    //监听单元格编辑
    table.on('edit(noticeTable)', function(obj){
        var value = obj.value //得到修改后的值
            ,data = obj.data //得到所在行所有键值
            ,field = obj.field; //得到字段
        //编辑
        var suo=data.flag;
        if(suo==1){
            Feng.error("已报销,无法修改!");
            table.reload(Notice.tableId);
            return;
        }
        var price=data.price;
        var num=data.num;
        var hj=parseFloat(price)*parseFloat(num);
        data.hj=hj;
        //return false;
        //var layerIndex = layer.load(3);
        $.ajax({
            type: "PUT",
            url: "/btyDzd/update",
            datatype: "json",
            data: data,
            success: function (data) {
                // if(data==2){
                //     Feng.error("已报销，不能修改");
                //     table.reload(Notice.tableId);
                // }
                //Feng.success("修改成功！");
                 table.reload(Notice.tableId);
            }, error: function () {
                Feng.error("修改失败！");
            }, complete: function (XHR, TS) {
                //layer.close(layerIndex);
                //50秒自动刷新一次
                //setTimeout(showTime,50000);
            }

        });
        //layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改为：'+ value);
    });


    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        // max: Feng.currentDate()
    });
    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    // $("#bz").on("input",function(e){
    //     //获取input输入的值
    //     Notice.search();
    // });
    // form.on('select(btyBxdaId)', function(data){
    //     Notice.search();
    // });
    form.on('select(flag)', function(data){
            Notice.search();
        });
    form.on('select(status)', function(data){
        Notice.search();
    });
    /**
     * 选中行效果
     */
    table.on('row(noticeTable)',function(obj){
        $(obj.tr).siblings().css("background-color","");
        obj.tr.css({"background-color":"#dadad9"});
    });

    /**
     * 检索配送人
     */

    var kjAjax = new $ax(Feng.ctxPath + "/btyPsda/getPsr");
    var kjResult = kjAjax.start();
    //console.info(kjResult);
    //console.log(kjResult);
    $.each(kjResult, function( index, val ) {
        $("#xm").append("<option value='"+val.xm+"'>"+val.xm+"</option>");
    } );
    form.render('select');
    /**
     * 检索姓名对应的店名
     */
    form.on('select(xm)', function(data){
        var eqy=data.value;
        if(eqy==""){
            $("#dm").html("");
            $("#dm").append("<option value=''>全部</option>");
            form.render('select');
        }else{
            var qyAjax = new $ax(Feng.ctxPath + "/btyPsda/getDm");
            qyAjax.set("xm",data.value);
            var qyResult = qyAjax.start();
            $("#dm").html("");
            $("#dm").append("<option value=''>全部</option>");
            $.each(qyResult, function( index, val ) {
                $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
            });
            form.render('select');
        }
    });
    /**
     * 检索店名对应的品项
     */
    form.on('select(dm)', function(data){
        var eqy=data.value;
        if(eqy==""){
            $("#px").html("");
            $("#px").append("<option value=''>全部</option>");
            form.render('select');
        }else{
            var qyAjax = new $ax(Feng.ctxPath + "/btyPsda/getPx");
            qyAjax.set("dm",data.value);
            var qyResult = qyAjax.start();
            $("#px").html("");
            $("#px").append("<option value=''>全部</option>");
            $.each(qyResult, function( index, val ) {
                $("#btyPsdaId").append("<option value='"+val.id+"'>"+val.px+"</option>");
            });
            form.render('select');
        }
    });
    // /**
    //  * 检索店名
    //  */
    // var languageAjax = new $ax(Feng.ctxPath + "/btyDzd/getDm");
    // var languageResult = languageAjax.start();
    // $.each(languageResult, function( index, val ) {
    //     $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    // } );
    // form.render('select');
    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox',LAY_CHECKED:true,hide:true},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            {align: 'center', type: "numbers", title: '序号'},
            {field: 'xm', align: "center", sort: true, title: '姓名'},
            {field: 'dm', align: "center", sort: true, title: '店名',width:180},
            {field: 'px', align: "center", sort: true, title: '品项',width:180,edit: 'text'},
            {field: 'dw', align: "center", sort: true, title: '单位',width:180,edit: 'text'},
            {field: 'num', align: "center", sort: true, title: '数量',edit: 'text'},
            {field: 'price', align: "center", sort: true, title: '单价',width:180,edit: 'text'},
            {field: 'hj', align: "center", sort: true, title: '合计',totalRow:true},
            {field: 'bz', align: "center", sort: true, title: '备注',edit: 'text'},
            {field: 'name', align: "center", sort: true, title: '操作员'},
            {field: 'createTime', align: "center", sort: true, title: '创建时间',width:180,},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['condition'] = $("#btyBxdaId").val();
        queryData['dm'] = $("#dm").val();
        queryData['status'] = $("#status").val();
        queryData['flag'] = $("#flag").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        queryData['kj'] = $("#kj").val();
        queryData['btyBrandId'] = $("#btyBrandId").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.openAddNotice = function () {
        var dm=$("#dm").val();
        if(dm == ""){
            Feng.error("请选择店名!");
            return;
        }
        var btyPsdaId=$("#btyPsdaId").val();
        if(btyPsdaId == ""){
            Feng.error("请输入品项!");
            return;
        }
        var dw=$("#dw").val();
        if(dw == ""){
            Feng.error("请输入单位!");
            return;
        }
        var num=$("#num").val();
        if(num == ""){
            Feng.error("请输入数量!");
            return;
        }
        var price=$("#price").val();
        if(price == ""){
            Feng.error("请输入单价!");
            return;
        }
        var bz = $("#bz").val();
        var hj=parseFloat(price)*parseFloat(num);
        var ajax = new $ax(Feng.ctxPath + "/btyDzd/add", function (data) {
            Feng.success("添加成功!");
            table.reload(Notice.tableId);
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });

        ajax.set("dm",dm);
        ajax.set("btyPsdaId",btyPsdaId);
        ajax.set("dw",dw);
        ajax.set("num",num);
        ajax.set("price",price);
        ajax.set("hj",hj);
        ajax.set("bz",bz);
        ajax.start();
        // func.open({
        //     height: 1000,
        //     title: '添加报销单',
        //     content: Feng.ctxPath + '/btyBxd/btyBxd_add',
        //     tableId: Notice.tableId
        // });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyDzd/delete", function (data) {
                if(data==2){
                    Feng.error("已报销,不允许删除!");
                    return;
                }
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除对账单 ?", operation);
    };
    /**
     * 点击修改档案
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 920,
            title: '修改对账单',
            content: Feng.ctxPath + "/btyDzd/btyDzd_update/" + data.id,
            tableId: Notice.tableId
        });
    };
    /**
     * 点击报销单审核
     * @param data 点击按钮时候的行数据
     */
    Notice.confirmOk = function () {
        var checkRows = table.checkStatus(Notice.tableId);
        var data = checkRows.data;
        var ids="";
        if (checkRows.data.length === 0) {
            Feng.error("请选择要确认的数据");
        }else{
            for (var i in data){
                ids+=data[i].id+",";
            }
            var ajax = new $ax(Feng.ctxPath + "/btyBxd/confirmOk", function (data) {
                if(data==1 || data==9){
                    Feng.error("已审核完成!请勿重复提交");
                    return;
                }
                Feng.success("审核成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("审核失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids",ids);
            ajax.start();
        }

    };
    /**
     * 导出excel按钮
     */
    Notice.exportExcel = function () {
        var checkRows = table.checkStatus(Notice.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyDzd/list',
        page: true,
        totalRow:true,	//开启合计行
        height: "full-155",
        cellMinWidth: 100,
        cols: Notice.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });
    // 审核按钮点击事件
    $('#btnOk').click(function () {
        Notice.confirmOk();
    });
    // 导出excel
    $('#btnExp').click(function () {
        Notice.exportExcel();
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }
    });
});
